<?php
class Mgallery extends CI_Model 
{
	public $table = 'tbl_gallery';
	public $table2 ='tbl_gallery_cat';
	public $pri_key="gallery_id";
	function __construct()
	{
		parent::__construct();
	}
	//****************** name cat ***************************//
	function namecat($catID)
	{
		$this->db->where('cat_id',$catID);
		$namecat = $this->db->get($this->table2);
		if($namecat != "")
		{
			$name = $namecat->row_array();
			if(!empty($name['cat_name']))
				return $name['cat_name'];
		}
	}
	//******************  gallery    *************************//
	function del_img($id)
	{
		$data = array('gallery_image'=>"");
		$this->db->where('gallery_id',$id);
		if($this->db->update($this->table,$data))
			return true;
		return false;
	}
	// lay chi tiet 
	public function info_detail($id,$table)
	{

		$this->db->where($this->pri_key, $id);
		return $this->db->get($table)->row_array();
	}
	function get_hot($lang)
	{
		$this->db->where('gallery_new',1);
		
		$this->db->where('gallery_lang',$lang);
			$this->db->where('gallery_status',1);
		$this->db->join("tbl_gallery_cat" , "cat_id = gallery_cate and cat_lang = $lang" );
		
		$query = $this->db->get('tbl_gallery');
		if($query->num_rows() >0)
			return $query->result_array();
		return false;
	}
	public function get_sub_cat ($lang)
	{
		$this->db->where('cat_lang',$lang);
			$this->db->where('cat_status',1);
		$query = $this->db->get('tbl_gallery_cat');
		if($query->num_rows() >0)
			return $query->result_array();
		return false;
	}
	

	public function get_gallery_detail($id,$lang)
	{
		$this->db->where($this->pri_key,$id);
	
		$this->db->where('gallery_lang',$lang);
		$this->db->where('gallery_status',1);
		$this->db->join("tbl_gallery_cat" , "cat_id = gallery_cate and cat_lang = $lang" );
		$query = $this->db->get('tbl_gallery');
		
		if($query->num_rows() >0)
			return $query->row_array();
		return false;
	}
		/*** index **///
	
	public function get_data($lang,$num='',$off='')
	{
		if($this->db->count_all($this->table) >0)
			//return $this->db->get($this->table)->result_array();
			{
			$this->db->limit($off, $num);
		$this->db->where('gallery_lang =',$lang);
		$this->db->where('gallery_status =',1);
		$this->db->join("tbl_gallery_cat" , "tbl_gallery_cat.cat_id = gallery_cate and cat_lang=$lang" );
	$this->db->order_by($this->pri_key,"DESC");
	return $this->db->get($this->table)->result_array();
			}
		return false;
	}
	
	public function get_gallery_cat($catid,$lang,$num,$off)
	{
		
	if ( $this->count_gallery_from_cate($catid) > 0)
	{
		$this->db->limit($off,$num);
		$this->db->where('gallery_cate',$catid);
		$this->db->where('gallery_lang',$lang);
		$this->db->where('gallery_status',1);
				$this->db->join("tbl_gallery_cat" , "cat_id = gallery_cate and cat_lang = $lang" );
		$query = $this->db->get('tbl_gallery');
		if($query->num_rows() >0)
			return $query->result_array();
	}
		return false;
		
	}
	public function get_manu($lang)
	{
		$this->db->where('ori_lang',$lang);
		$query = $this->db->get('tbl_origin');
		if($query->num_rows() >0)
			return $query->result_array();
		return false;
	}
	
	public function gallery_from_cate($num,$off,$id,$dd="") 
	{
			$this->db->select('gallerys.name,gallerys.id,gallerys.promotion,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->limit($off,$num);
		$this->db->where("sub_id =",$id);
			$this->db->join("unit" , "unit.id = unit" );
		if($dd != 0 && $dd != "")
		{
		$this->db->where("dd".$dd." =",1);
		}$this->db->order_by($this->table.".".$this->pri_key, "desc"); 
		return $query =$this->db->get($this->table)->result_array();
	}
		public function del_item($id)
	{
		$this->db->where($this->pri_key, $id);
		$this->db->delete($this->table); 
			
	}
	
	

	public function get_cate()
	{
		return $query = $this->db->get($this->table_cate)->result_array();
	}
	
	public function get_unit()
	{
	return $this->db->get("unit")->result_array();
	}
	
	
	function list_data_gallery_cat($num,$off,$cat)
	{
		$cat = (int)$cat;
		$this->db->limit($off,$num);
	
		$this->db->join("tbl_gallery_cat as a" , "a.cat_id = gallery_cate" );
		
			
		$this->db->order_by($this->pri_key, "desc"); 

		$query = $this->db->get($this->table);
		if($query->num_rows()  >0)
			{
			
					
				
				return $query->result_array();
			}
		return false;
	}
	
	
	
	function list_data($catID,$num,$off)
	{
		$this->db->limit($off,$num);
		if($this->db->count_all($this->table) >0)
			{
				$this->db->where("gallery_cate",$catID);
				$this->db->order_by($this->pri_key, "desc"); 
				return $this->db->get($this->table)->result_array();
			}
		return false;
	}
		function insert_gallery($data)
	{
		
		if($this->db->insert($this->table,$data))
			return true;
		return false;
	
	}
	//***************** update gallery category **********************//
	function update_gallery($data,$id)
	{
		if(is_numeric($id) && $id !='')
		{
			$this->db->where($this->pri_key,$id);
			if($this->db->update($this->table,$data))
				return true;
			return false;
			
		}
		return false;
	}
	//****************** delete *******************************//
	function delete()
	{
		if (isset($_POST['checkbox1']))
		{
			foreach($_POST['checkbox1'] as $id) 
			{
				$row = $this->info_from_id($id,$this->table);
				$row = $row[0];
				if (!empty($row['gallery_image'])) unlink("./upload/gallery/".$row['gallery_image']);

				$this->db->where($this->pri_key, $id);
				$this->db->delete($this->table); 

			}			

		}

	}
	public function get_last_id()
	{
		$this->db->order_by($this->pri_key." DESC");
		
		return $this->db->get($this->table)->row_array();
		
		
	}
	//get gallery from cate
	public function get_gallery_from_cate($num,$off,$id)
	{
	$this->db->select('sub_categories.*,gallerys.*,categories.*,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->where("categories.cate_id",$id);
		$this->db->join("sub_categories","sub_categories.sub_cate_id = gallerys.sub_id");
	$this->db->join("categories","categories.cate_id = sub_categories.cate_id");
	$this->db->join("unit" , "unit.id = unit" );
	$this->db->limit($off,$num);
//	print_r($this->db->where());
	$query =$this->db->get("gallerys");
	if($query->num_rows() >0)
	{
		return $query->result_array();
	}
	
	else
	return false;
	
	}
	public function count_gallery_from_cate($id)
	{
	//$this->db->select('sub_categories.*,gallerys.*,categories.*,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->where("gallery_cate",$id);
///		$this->db->join("sub_categories","sub_categories.sub_cate_id = gallerys.sub_id");
	$this->db->join("tbl_gallery_cat","cat_id = gallery_cate");
	
	$query =$this->db->get("tbl_gallery");
	
		return $query->num_rows();

	
	}
	function info_from_cat($catid,$lang =1)
	{
		if(is_numeric($catid) && $catid !="")
		{
			$where = array('cat_id' => $catid,'cat_lang'=>$lang);
			$this->db->where($where);
			
		//	$this->db->join("tbl_gallery_cat" , "cat_id = gallery_cate" );

	//		print_r($this->db->where());
			$query =$this->db->get('tbl_gallery_cat'); 
			return $query->result_array();
		}
		else
			return false;
	
	}
	
	// get info from id 
	public function info_from_id($id,$table)
	{
		if($table == 'tbl_gallery_cat')
		{
			$pri_key=$this->pri_key;
		}
	
		if(is_numeric($id) && $id !="")
		{
			$where = array($this->table.".".$this->pri_key => $id);
			$this->db->where($where);
			
		//	$this->db->join("tbl_gallery_cat" , "cat_id = gallery_cate" );

	//		print_r($this->db->where());
			$query =$this->db->get($table); 
			return $query->result_array();
		}
		else
			return false;
	}
	public function count_event($type)
	{
		$this->db->select('*')->from($this->table)->where($type,1);	
			$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
		$this->db->join("unit" , "unit.id = unit" );	
			
		$query = $this->db->get();
		return $query->num_rows();
	}
	
	public function get_event($type,$num='',$off='')
	{
		

		$this->db->select('gallerys.name,gallerys.id,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name')->from($this->table)->where($type,1)->limit($off, $num);
			$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
		$this->db->join("unit" , "unit.id = unit" );	
		$this->db->order_by($this->table.".".$this->pri_key." desc");
		$query = $this->db->get();
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
	
	}
	function others_gallery($cateid,$id)
	{
		$this->db->select('sub_categories.*,gallerys.*,gallerys.id,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		$this->db->where("sub_id",$cateid);
		$this->db->where("gallerys.id != ".$id);
		$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
			$this->db->join("unit" , "unit.id = unit" );
		$this->db->limit(10);
		$this->db->order_by($this->table.".".$this->pri_key." desc");
		$query = $this->db->get($this->table);
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
	}

	//dem so record cua sub id
	function count_gallery_sub($sub)
	{
		$this->db->where("gallerys.sub_id",$sub);
		$this->db->join("sub_categories" , "sub_cate_id = gallerys.sub_id" );
		$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
		$this->db->join("unit" , "unit.id = unit" );
		$query=  $this->db->get($this->table);
		return $query->num_rows() ;
	}
	// dem so record 
	function count_all($catID,$table)
	{
		$this->db->where("gallery_cate",$catID);
		return $this->db->count_all_results($table);
		
	}
		function count_by_cate($cate)
	{
		$this->db->where("sub_id",$cate);
		
		$this->db->join("unit" , "unit.id = unit" );	
		$query = $this->db->get($this->table);
		return $query->num_rows();
	}
	
	function search ($keyword , $type,$num='',$off='')
	{
		//$this->db->select('*')->from($this->table)->where($type,)->limit($off, $num);
		$this->db->select('gallerys.*,gallerys.id,image,price_old,price_new,unit.id as unit_id,unit.name as unit_name');
		if($type !=0)
		{
			$this->db->where("sub_id",$type);
		}
			$this->db->like("gallerys.name",$keyword);
	$this->db->join("sub_categories" , "sub_cate_id = sub_id" );
				$this->db->join("categories" , "categories.cate_id = sub_categories.cate_id" );
			$this->db->join("unit" , "unit.id = unit" );
			$this->db->limit($off, $num);
		$this->db->order_by($this->pri_key." desc");
		$query= $this->db->get($this->table);
		//print_r($this->db->where());
		//$query = $this->db->get();
		if($query->num_rows() >0)
		{
			return $query->result_array();
		}
		else
			return false;
		
	}


	function insert_order($data)
	{
		if($this->db->insert('tbl_order',$data))
			return true;
		return false;
	
	}
	function get_detail_cat($catID)
	{
		$this->db->where("gallery_cate",$catID);
		$this->db->where("gallery_status",1);
		return $this->db->get("tbl_gallery")->result_array();
	}
 

}//end model class





/* End of file mhumans.php */

/* Location: ./system/application/models/mhumans.php */

?>